<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tender extends MY_Controller {
		
	function __construct(){
		parent::__construct();
	}	
	
	/***
	 * by liub 20150107 后台集中一次性投标
	 * */
	function  tender_all(){
		/**
		 * 1.根据投标标的ID查询所有投资者的 user_id,money(实际金额),order_id,fstatus = 0 
		 * 
		 * 2.循环投标
		 * 
		 * 3.成功之外的状态，将该笔订单状态置: 3
		 * 
		 * */
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->get_all_bow();;// 被投标的
		
		foreach ($brow_arr as $borw_msg){
			$falg = self::do_ki($borw_msg['id']);
			if( 1== $falg ){
				self::do_tender($borw_msg['id'],$borw_msg['user_id']);		
			}
		}
			
	}
	
	//***** 投标
		//***** 投标
	private function do_tender($borw_id,$borw_user){
		$this->load->model("Tender_model");
		$bow_tenders = $this->Tender_model->get_bow_tender($borw_id);
		
		$config_1 = array();
		$config['user_id']		= null;	// 投资者
		$config['amount'] 		= null;	// 实投金额
		$config['fee'] 			= null;
		$config['is_voucher'] 	= null;
		$config['special'] 		= null;
		$this->load->library('baofooSdk/dto/request/Action',$config,'action');
		$this->load->library('baofooSdk/dto/request/P2pRequestDto',$config_1,'p2pRequestDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
				// 事务开启
				$flag = FALSE;
		$this->db->trans_strict(FALSE);
		$this->db->trans_begin();
		foreach ($bow_tenders as $bow_tender){
			if( $bow_tender['fstatus'] ==1 && $bow_tender['bf_trade_status'] ==3){
				continue;
			}
			unset($config_1);
			$actions = Array();
			$this->action->__set('user_id',$bow_tender['user_id']);// 投资者ID
			$this->action->__set('amount',$bow_tender['account']);// 实投金额
			$this->action->__set('fee',null);
			$this->action->__set('is_voucher',null);
			$this->action->__set('special',null);
			array_push($actions,object_to_array($this->action));
			// 暂无担保人,略过
			
			$order_id = self::createOrderId(1);
			$this->p2pRequestDto->__set('merchant_id',MERCHANT_ID);
			$this->p2pRequestDto->__set('action_type',1);// 投标
			$this->p2pRequestDto->__set('order_id',$order_id);
			$this->p2pRequestDto->__set('cus_id',$borw_id);
			$this->p2pRequestDto->__set('cus_name',$borw_id);
			$this->p2pRequestDto->__set('brw_id',$borw_user);
			$this->p2pRequestDto->__set('req_time',strtotime(date("Y-m-d H:i:s")).'000');
			$this->p2pRequestDto->__set('fee',0);
			$this->p2pRequestDto->__set('actions',$actions);

			$result = $this->br->api_P2pRequest($this->p2pRequestDto);
			
			// print_r($result);
			if($result['code'] != 'CSD000'){//失败
				$flag = TRUE;
				$update = " update rsh_borrow_tender set bf_trade_orderid = '{$order_id}', bf_result_code = '{$result['code']}' where id={$bow_tender[id]}";
			}else{//成功
				$update = " update rsh_borrow_tender set bf_trade_orderid = '{$order_id}', bf_trade_status = 3 ,bf_result_code = '{$result['code']}' where id={$bow_tender[id]}";
			}
			//TODO 事务处理 100笔提交一次
			
			// print_r($update);
			$this->db->query($update);
		}
		
		// 事务提交
		if($this->db->trans_status() === TRUE) {
					
        	$this->db->trans_commit();
        	$this->load->model('log_model');
        	$this->log_model->add_log('[自动审核][投标][成功][ID=='.$borw_id.']');
        	return 1;
    	} else {
      		$this->db->trans_rollback();
      		$this->load->model('log_model');
      		$this->log_model->add_log('[自动审核][投标][失败][ID=='.$borw_id.']');
        	return 0;
   		}
		
	}
	
	
	// 满标
	function full_borw_all(){
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->get_all_full_bow();;// 需要满标标的
		foreach ($brow_arr as $borw_msg){
			 self::do_full($borw_msg['id'],$borw_msg['user_id']);		
		}
	}

	private  function do_full($bow_id,$bow_user){
		$actions = array();
		// 借款人
		$config['user_id']		= null;// 借款用户ID
		
		// {（投资总额 - 商户手续费） - 流标总额 }  = = 满标金额  
		$this->load->model("Tender_model");
		$amount = $this->Tender_model->getFullMoney($bow_id,0);;// 满标标的
		$config['amount'] 		= null;// 满标的金额(不一定等于标的理财金额),获取实投金额
		$config['fee'] 			= null;	// 一点投满标所收手续费目前为0 
		$config['is_voucher'] 	= null; 	// 借款人唯一标记，其他为null
		$config['special'] 		= null;	// 还款人唯一标记，其他为null
		$this->load->library('baofooSdk/dto/request/Action',$config,'action');
		
		
		$this->action->__set('user_id',$bow_user);
		$this->action->__set('amount',$amount);
		$this->action->__set('fee',0);
		$this->action->__set('is_voucher',0);
		$this->action->__set('special',null);
		
		array_push($actions,object_to_array($this->action));
		// 担保人信息可封装成Action,暂无担保人信息略过
		$order_id = self::createOrderId(2);
		// 交易DTO
		$config_1['merchant_id'] 	= MERCHANT_ID;
		$config_1['action_type'] 	= "2";// 投标为1，满标为2，流标为3，还标为4
		$config_1['order_id'] 		= $order_id ;
		
		$config_1['cus_id'] 		= $bow_id ;		// 标的ID
		$config_1['cus_name'] 		= $bow_id;		// 标的名称
		$config_1['brw_id'] 		= $bow_user;	// 借款人ID(唯一)
		
		$config_1['req_time'] 		= strtotime(date("Y-m-d H:i:s")).'000';// 13位
		$config_1['fee'] 			= "0";// 一点投平台所收手续费
		$config_1['actions'] 		= $actions;
		$this->load->library('baofooSdk/dto/request/P2pRequestDto',$config_1,'p2pRequestDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$result = $this->br->api_P2pRequest($this->p2pRequestDto);
		
		//print_r($result);
		if($result['code'] != 'CSD000'){// 失败			
			$update = " update rsh_borrow set  bf_trade_orderid = '{$order_id}',bf_trade_status = 2 , bf_result_code = '{$result['code']}' where id={$bow_id}";
		}else{// 成功	
			$update = " update rsh_borrow set  bf_trade_orderid = '{$order_id}',bf_trade_status = 3 ,fstatus = 3, bf_result_code = '{$result['code']}' where id={$bow_id}";
			
			// 新增投资积分处理
			self::updateInvistScore($bow_id);
		}
		//TODO 事务处理 100笔提交一次
		//print_r($update);
		$this->db->query($update);
		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][自动满标]');
	}
	
	//***** by liub 根据交易类型生成订单ID
	private function createOrderId($type){
		// 时间戳
		$orderid = time();
		// 生成3位随机数字
		$num = create_num(3);// 模拟毫秒数
		// 生成订单id
		$orderid =$type.'_'.$orderid.$num;
		
		return $orderid;
	}
	
		// 更新用户每日投资积分
	private function updateInvistScore($borw_id){
		// 投资积分 = (目前投资总额/365)*(投资天数 = 已投标的理财期限 )
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->cul_score($borw_id);// 需要满标标的
	}


	private function do_ki($id){
			$fstatus 			= 3;
			$repayment_remark 	= "满标审核";
  		$data['id']					= $id;
  		$data['ip']					= $this->js->get_ip();
  		$data["fstatus"]			= $fstatus;
  		$data["repayment_remark"]	= $repayment_remark;
  		$this->load->model('Borrow_model');
  		$flag = 0;
  		$flag = $this->Borrow_model->zhs_borrow_review_true($data);
  		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][满标]');
  		
			return $flag;
	
	}
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tender extends MY_Controller {
    	
	function __construct(){
		parent::__construct();
	}	
	
	/***
	 * by liub 20150107 后台集中一次性投标
	 * */
	function  tender_all(){
		/**
		 * 1.根据投标标的ID查询所有投资者的 user_id,money(实际金额),order_id,fstatus = 0 
		 * 
		 * 2.循环投标
		 * 
		 * 3.成功之外的状态，将该笔订单状态置: 3
		 * 
		 * */
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->get_all_bow();;// 被投标的
		
		foreach ($brow_arr as $borw_msg){
			$falg = self::do_ki($borw_msg['id']);
			if( 1== $falg ){
				self::do_tender($borw_msg['id'],$borw_msg['user_id']);		
			}
		}
			
	}
	
	//***** 投标
		//***** 投标
	private function do_tender($borw_id,$borw_user){
		$this->load->model("Tender_model");
		$bow_tenders = $this->Tender_model->get_bow_tender($borw_id);
		
		$config_1 = array();
		$config['user_id']		= null;	// 投资者
		$config['amount'] 		= null;	// 实投金额
		$config['fee'] 			= null;
		$config['is_voucher'] 	= null;
		$config['special'] 		= null;
		$this->load->library('baofooSdk/dto/request/Action',$config,'action');
		$this->load->library('baofooSdk/dto/request/P2pRequestDto',$config_1,'p2pRequestDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
				// 事务开启
				$flag = FALSE;
		$this->db->trans_strict(FALSE);
		$this->db->trans_begin();
		foreach ($bow_tenders as $bow_tender){
			if( $bow_tender['fstatus'] ==1 && $bow_tender['bf_trade_status'] ==3){
				continue;
			}
			unset($config_1);
			$actions = Array();
			$this->action->__set('user_id',$bow_tender['user_id']);// 投资者ID
			$this->action->__set('amount',$bow_tender['account']);// 实投金额
			$this->action->__set('fee',null);
			$this->action->__set('is_voucher',null);
			$this->action->__set('special',null);
			array_push($actions,object_to_array($this->action));
			// 暂无担保人,略过
			
			$order_id = self::createOrderId(1);
			$this->p2pRequestDto->__set('merchant_id',MERCHANT_ID);
			$this->p2pRequestDto->__set('action_type',1);// 投标
			$this->p2pRequestDto->__set('order_id',$order_id);
			$this->p2pRequestDto->__set('cus_id',$borw_id);
			$this->p2pRequestDto->__set('cus_name',$borw_id);
			$this->p2pRequestDto->__set('brw_id',$borw_user);
			$this->p2pRequestDto->__set('req_time',strtotime(date("Y-m-d H:i:s")).'000');
			$this->p2pRequestDto->__set('fee',0);
			$this->p2pRequestDto->__set('actions',$actions);

			$result = $this->br->api_P2pRequest($this->p2pRequestDto);
			
			// print_r($result);
			if($result['code'] != 'CSD000'){//失败
				$flag = TRUE;
				$update = " update rsh_borrow_tender set bf_trade_orderid = '{$order_id}', bf_result_code = '{$result['code']}' where id={$bow_tender[id]}";
			}else{//成功
				$update = " update rsh_borrow_tender set bf_trade_orderid = '{$order_id}', bf_trade_status = 3 ,bf_result_code = '{$result['code']}' where id={$bow_tender[id]}";
			}
			//TODO 事务处理 100笔提交一次
			
			// print_r($update);
			$this->db->query($update);
		}
		
		// 事务提交
		if($this->db->trans_status() === TRUE) {
					
        	$this->db->trans_commit();
        	$this->load->model('log_model');
        	$this->log_model->add_log('[自动审核][投标][成功][ID=='.$borw_id.']');
        	return 1;
    	} else {
      		$this->db->trans_rollback();
      		$this->load->model('log_model');
      		$this->log_model->add_log('[自动审核][投标][失败][ID=='.$borw_id.']');
        	return 0;
   		}
		
	}
	
	
	// 满标
	function full_borw_all(){
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->get_all_full_bow();;// 需要满标标的
		foreach ($brow_arr as $borw_msg){
			 self::do_full($borw_msg['id'],$borw_msg['user_id']);		
		}
	}

	private  function do_full($bow_id,$bow_user){
		$actions = array();
		// 借款人
		$config['user_id']		= null;// 借款用户ID
		
		// {（投资总额 - 商户手续费） - 流标总额 }  = = 满标金额  
		$this->load->model("Tender_model");
		$amount = $this->Tender_model->getFullMoney($bow_id,0);;// 满标标的
		$config['amount'] 		= null;// 满标的金额(不一定等于标的理财金额),获取实投金额
		$config['fee'] 			= null;	// 一点投满标所收手续费目前为0 
		$config['is_voucher'] 	= null; 	// 借款人唯一标记，其他为null
		$config['special'] 		= null;	// 还款人唯一标记，其他为null
		$this->load->library('baofooSdk/dto/request/Action',$config,'action');
		
		
		$this->action->__set('user_id',$bow_user);
		$this->action->__set('amount',$amount);
		$this->action->__set('fee',0);
		$this->action->__set('is_voucher',0);
		$this->action->__set('special',null);
		
		array_push($actions,object_to_array($this->action));
		// 担保人信息可封装成Action,暂无担保人信息略过
		$order_id = self::createOrderId(2);
		// 交易DTO
		$config_1['merchant_id'] 	= MERCHANT_ID;
		$config_1['action_type'] 	= "2";// 投标为1，满标为2，流标为3，还标为4
		$config_1['order_id'] 		= $order_id ;
		
		$config_1['cus_id'] 		= $bow_id ;		// 标的ID
		$config_1['cus_name'] 		= $bow_id;		// 标的名称
		$config_1['brw_id'] 		= $bow_user;	// 借款人ID(唯一)
		
		$config_1['req_time'] 		= strtotime(date("Y-m-d H:i:s")).'000';// 13位
		$config_1['fee'] 			= "0";// 一点投平台所收手续费
		$config_1['actions'] 		= $actions;
		$this->load->library('baofooSdk/dto/request/P2pRequestDto',$config_1,'p2pRequestDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$result = $this->br->api_P2pRequest($this->p2pRequestDto);
		
		//print_r($result);
		if($result['code'] != 'CSD000'){// 失败			
			$update = " update rsh_borrow set  bf_trade_orderid = '{$order_id}',bf_trade_status = 2 , bf_result_code = '{$result['code']}' where id={$bow_id}";
		}else{// 成功	
			$update = " update rsh_borrow set  bf_trade_orderid = '{$order_id}',bf_trade_status = 3 ,fstatus = 3, bf_result_code = '{$result['code']}' where id={$bow_id}";
			
			// 新增投资积分处理
			self::updateInvistScore($bow_id);
		}
		//TODO 事务处理 100笔提交一次
		//print_r($update);
		$this->db->query($update);
		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][自动满标]');
	}
	
	//***** by liub 根据交易类型生成订单ID
	private function createOrderId($type){
		// 时间戳
		$orderid = time();
		// 生成3位随机数字
		$num = create_num(3);// 模拟毫秒数
		// 生成订单id
		$orderid =$type.'_'.$orderid.$num;
		
		return $orderid;
	}
	
		// 更新用户每日投资积分
	private function updateInvistScore($borw_id){
		// 投资积分 = (目前投资总额/365)*(投资天数 = 已投标的理财期限 )
		$this->load->model("Tender_model");
		$brow_arr = $this->Tender_model->cul_score($borw_id);// 需要满标标的
	}


	private function do_ki($id){
			$fstatus 			= 3;
			$repayment_remark 	= "满标审核";
  		$data['id']					= $id;
  		$data['ip']					= $this->js->get_ip();
  		$data["fstatus"]			= $fstatus;
  		$data["repayment_remark"]	= $repayment_remark;
  		$this->load->model('Borrow_model');
  		$flag = 0;
  		$flag = $this->Borrow_model->zhs_borrow_review_true($data);
  		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][满标]');
  		
			return $flag;
	
	}
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}